package cn.ailearn.security.handler;

import cn.ailearn.result.Result;
import cn.ailearn.utils.web.WebUtils;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName AccessDeniedHandlerImpl
 * @Description 权限不足(异常)失败处理类
 * @Author WDEMO
 * @Date 2025/10/23 18:08
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Result result = Result.error("权限不足，请联系管理员");
        String jsonResult = JSONUtil.toJsonStr(result);
        WebUtils.writeResponse(response,jsonResult);
    }
}
